//app.js
import wxRequest from "/utils/http.js"
App({
    onLaunch: function () {

        // 微信登录
        wx.login({
            success: res => {
                let that = this;
                wx.removeStorageSync("token");
                wx.request({
                    url: this.globalData.baseUrl + '/sys-user/wxLogin',
                    method: "POST",
                    data: {
                        code: res.code
                    },
                    header: {
                        "Content-Type": "application/x-www-form-urlencoded"
                    },
                    success: function (res) {
                        if (res.data.code === 2200) {
                            wx.setStorageSync('token', res.data.result);
                            wx.request({
                                url: that.globalData.baseUrl + '/sys-user/getUserInfo',
                                method: "GET",
                                header: {
                                    "token": wx.getStorageSync('token')
                                },
                                success: function (res) {
                                    if (res.data.code === 200) {
                                        wx.setStorageSync('avatarUrl', res.data.result.avatarUrl);
                                        wx.setStorageSync('nickName', res.data.result.nickName);
                                    }
                                }
                            })
                            wx.showToast({
                                title: res.data.msg,
                                icon: 'none',
                                duration: 3000
                            });
                        } else if (res.data.code === 2201) {
                            wx.showToast({
                                title: res.data.msg,
                                icon: 'none',
                                duration: 3000
                            });
                        }
                    }
                })
            }
        })
        // 获取用户信息
        wx.getSetting({
            success: res => {
                if (res.authSetting['scope.userInfo']) {
                    // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                    wx.getUserInfo({
                        success: res => {
                            // 可以将 res 发送给后台解码出 unionId
                            this.globalData.userInfo = res.userInfo
                            // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
                            // 所以此处加入 callback 以防止这种情况
                            if (this.userInfoReadyCallback) {
                                this.userInfoReadyCallback(res)
                            }
                        }
                    })
                }
            }
        })
        wx.getSystemInfo({
            success: e => {
                this.globalData.StatusBar = e.statusBarHeight;
                let custom = wx.getMenuButtonBoundingClientRect();
                this.globalData.Custom = custom;
                this.globalData.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
            }
        })
        wxRequest.defaults.baseURL = this.globalData.baseUrl;
        wxRequest.defaults.headers.common['token'] = wx.getStorageSync("token");
    },
    globalData: {
        userInfo: null,
        baseUrl: 'https://frp.yoonada.com/api'
        // baseUrl: 'http://127.0.0.1:9999/api'
    }
})
